package cn.stylefeng.guns.modular.hx.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicGoods;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicPositionGoods;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicGoodsPositionVo;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicPositionGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicPositionGoodsResult;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicGoodsService;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicPositionGoodsService;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageStockService;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationYtParam;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 库位商品关联表控制器
 *
 * @author zhucx
 * @Date 2023-09-06 14:43:07
 */
@Controller
@RequestMapping("/hxBasicPositionGoods")
public class HxBasicPositionGoodsController extends BaseController {

    private String PREFIX = "/hxBasicPositionGoods";

    @Autowired
    private HxBasicPositionGoodsService hxBasicPositionGoodsService;

    @Autowired
    private HxBasicGoodsService hxBasicGoodsService;

    @Autowired
    private BasicPositionService basicPositionService;

    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     * fz
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/hxBasicPositionGoods.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxBasicPositionGoods_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxBasicPositionGoods_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/addItem")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,timeout=150)
    public ResponseData addItem(HxBasicPositionGoodsParam hxBasicPositionGoodsParam, Long[] gids) {
        User user = userService.getcurrentUser();
    	BasicPosition basicPosition = basicPositionService.getById(hxBasicPositionGoodsParam.getPositionId());
        BasicArea basicArea = this.basicAreaService.getById(basicPosition.getPositionAreaId());
        List<HxBasicPositionGoods> addList = new ArrayList();
        StringBuffer err = new StringBuffer();
        for (Long gid : gids) {
    		HxBasicPositionGoodsParam check = new HxBasicPositionGoodsParam();
    		check.setPositionId(hxBasicPositionGoodsParam.getPositionId());
    		check.setGoodsId(gid);
    		HxBasicPositionGoodsResult goodsResult = hxBasicPositionGoodsService.findBySpec(check);
    		if(goodsResult == null) {
    			HxBasicGoods hxBasicGoods = hxBasicGoodsService.getById(gid);
    			if(ToolUtil.isNotEmpty(basicArea.getDismantlingParts()) && !hxBasicGoods.getDismantlingParts().equals(basicArea.getDismantlingParts())){
    			    if("是".equals(hxBasicGoods.getDismantlingParts())){
                        err.append("商品[").append(hxBasicGoods.getGoodsName());
                        err.append("]是拆零商品，不能绑定非拆零库区");
                    }else{
                        err.append("商品[").append(hxBasicGoods.getGoodsName());
                        err.append("]是非拆零商品，不能绑定拆零库区");
                    }
    			    return ResponseData.error(err.toString());
                }
                HxBasicPositionGoods add = new HxBasicPositionGoods();
    			add.setWarehouseId(Long.parseLong(basicPosition.getWarehouseId().toString()));
    			add.setAreaId(Long.parseLong(basicPosition.getPositionAreaId().toString()));
    			add.setAreaName(basicPosition.getPositionAreaName());
    			add.setPositionId(hxBasicPositionGoodsParam.getPositionId());
    			add.setPositionNumber(basicPosition.getPositionNumber());
                add.setPositionPickingorder(basicPosition.getPositionPickingorder());
    			add.setGoodsId(gid);
    			add.setGoodsName(hxBasicGoods.getGoodsName());
    			add.setGoodsNumber(hxBasicGoods.getGoodsNumber());
    			add.setGoodsPcode69(hxBasicGoods.getGoodsPcode69());
    			add.setGoodsSpecs(hxBasicGoods.getGoodsSpecs());
    			add.setAnquanNum(hxBasicGoods.getAnquanNum());
    			add.setPsUnit(hxBasicGoods.getPsUnit());
    			add.setPsRate(hxBasicGoods.getPsRate());
    			add.setJibenUnit(hxBasicGoods.getJibenUnit());
                add.setOperator(user.getName());
                addList.add(add);
    		}
		}

        this.hxBasicPositionGoodsService.saveBatch(addList);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxBasicPositionGoodsParam hxBasicPositionGoodsParam) {
    	hxBasicGoodsService.lambdaUpdate()
    	.eq(HxBasicGoods::getId, hxBasicPositionGoodsParam.getGoodsId())
    	.set(HxBasicGoods::getAnquanNum, hxBasicPositionGoodsParam.getAnquanNum())
    	.update();
        this.hxBasicPositionGoodsService.update(hxBasicPositionGoodsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxBasicPositionGoodsParam hxBasicPositionGoodsParam) {
        this.hxBasicPositionGoodsService.delete(hxBasicPositionGoodsParam);
        return ResponseData.success();
    }

    /**
     * 批量删除接口
     * @author zhucx
     * @Date 2022-12-14
     */
    @RequestMapping("/btnDelete")
    @ResponseBody
    public ResponseData btnDelete(Integer[] ids) {
    	for (Integer id : ids) {
    		HxBasicPositionGoods hxBasicPositionGoods = hxBasicPositionGoodsService.getById(id);
    		
    		HxStorageStockParam hxStorageStockParam = new HxStorageStockParam();
    		hxStorageStockParam.setGoodsId(hxBasicPositionGoods.getGoodsId());
    		hxStorageStockParam.setPositionId(hxBasicPositionGoods.getPositionId());
    		HxStorageStockResult stockResult = hxBasicPositionGoodsService.findStorageStockBySpec(hxStorageStockParam);
    		if(stockResult != null) {
    			return ResponseData.error(hxBasicPositionGoods.getGoodsName()+"无法删除，系统存在库存");
    		}
    		
    		HxBasicPositionGoodsParam hxBasicPositionGoodsParam = new HxBasicPositionGoodsParam();
    		hxBasicPositionGoodsParam.setId(id);
    		this.hxBasicPositionGoodsService.delete(hxBasicPositionGoodsParam);
		}
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxBasicPositionGoodsParam hxBasicPositionGoodsParam) {
        HxBasicPositionGoods detail = this.hxBasicPositionGoodsService.getById(hxBasicPositionGoodsParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-09-06
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxBasicPositionGoodsParam hxBasicPositionGoodsParam,String condition ) {
        return this.hxBasicPositionGoodsService.findPageBySpec(hxBasicPositionGoodsParam,condition);
    }


    /**
     * 查看详情接口
     */
    @RequestMapping("/btnGoodsPositionCode")
    @ResponseBody
    public ResponseData btnGoodsPositionCode(HxBasicGoodsPositionVo hxBasicGoodsPositionVo) {
        if(ToolUtil.isEmpty(hxBasicGoodsPositionVo.getIds())){
            return ResponseData.error("请选择要打印的记录");
        }
        List<HxBasicPositionGoods> list = this.hxBasicPositionGoodsService.lambdaQuery()
                .in(HxBasicPositionGoods::getPositionId, hxBasicGoodsPositionVo.getIds().split(","))
                .list();
        return ResponseData.success(list);
    }


    /**
     * 创建条码
     */
    @RequestMapping(value = "/getDpsCode", method = RequestMethod.GET)
    @ResponseBody
    public void getDpsCode(String id, String name, HttpServletRequest request, HttpServletResponse response) {
        try {
            //logo
            File logoFile = new File("");
            String url = id;
            QRCodeUtil.drawLogoCode_128(logoFile, url, name, 350, 70, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


